package com.yjhb.android.sbz.http;

import com.jojo.android.zxlib.entity.Result;
import com.jojo.android.zxlib.tool.ZxLogTool;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 创建者 zx
 * 创建时间 2020/11/17 14:50
 */
public abstract class HttpObserver<T> implements Observer<Result<T>> {
    private final String TAG = "HttpObserver";

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(Result<T> tResult) {
        try {
            if (tResult.getCode() == 200) {
                onResult(tResult.getData());
            } else {
                if (tResult.getCode() == 403 || tResult.getCode() == 604 || tResult.getCode() == 606) {
//                    ZxToast.showINFOBottomToast("请先登录~");
//                    ZxActivityTool.skipActivity(App.getInstance(), LoginActivity.class);
                }
                onError(tResult.getCode(), tResult.getMessage());
            }
        } catch (Exception e) {
            ZxLogTool.err(e);
            onError(-1, e.getMessage());
        }
    }

    @Override
    public void onError(Throwable e) {
        try {
            ZxLogTool.err(e);
            onError(-1, e.getMessage());
        } catch (Exception e1) {
            ZxLogTool.err(e1);
        }
    }

    @Override
    public void onComplete() {

    }

    public abstract void onResult(T data);

    public abstract void onError(int code, String msg);
}
